<?php

namespace Ctrl;

use Lib;
use Lib\Paas\Login;
use Model\HeroModel;
use Model\ItemModel;
use Lib\Service\JsonService;
use Model\RaceModel;

class Hero extends Lib\Base\Ctrl
{
    private $db;
    private $version;
    private $season;
    private $where;
    private $heroModel;

    public function __construct()
    {
        //校验管理员权限
        $roleService = new Lib\Paas\Role();
        $checkAdmin  = $roleService->getRoleInfo();
        //        if ($checkAdmin['role'] != ROLE_ID) {
        //            die('抱歉！您没有访问权限');
        //        }
        //        if (!in_array(MODULE_ID, $checkAdmin['moduleID']) ) {
        //            die('抱歉！您没有访问权限');
        //        }
        $this->db = Lib\Db\DbModel::init();

        //获取当前版本
        $this->version = $this->db->table('tbversion')->where(['isCurrent' => 1])->find('id');
        $this->season  = $this->db->table('tbseason')->where(['isCurrent' => 1])->find('id');
        /*if (empty($this->version) || empty($this->season)) {
            header("Location: ".U('main/main'));
        }*/
        $this->where = [
            'versionId' => $this->version['id'],
            'seasonId'  => $this->season['id'],
            'isDelete'  => 0
        ];
        $this->assign('iActId', '1001');
        $this->heroModel = new HeroModel();
    }

    public function getMini()
    {
        return $this->db->table('tbheromini')->select();
    }

    //种族视图首页
    public function index()
    {
        $miniKeywords = I('post.miniKeywords');
        if ($miniKeywords && !empty($miniKeywords)) {
            $miniWhere = ['name' => ['like', '%'.$miniKeywords.'%']];
        } else {
            $miniWhere = [];
        }

        //获取种族所有种族信息
        $mini = $this->db->table('tbheromini')->where($miniWhere)->select();

        if (I('post.')) {
            $this->ajaxSuccess("成功", $mini);
        }

        $type = $this->db->table('tbherotype')->select();

        $this->assign('mini', $mini);
        $this->assign('type', $type);
        $this->assign('shape', HeroModel::SHAPE_ARRAY);
        $this->assign('quality', HeroModel::QUALITY_ARRAY);

        $this->display('Admin/Hero/index');
    }

    //根据英雄所属获取对应的英雄
    public function getHeroByMiniId()
    {
        $miniId           = I('miniId');
        $where            = $this->where;
        $where ['miniId'] = $miniId;

        $keywords = I('keywords');
        if ($keywords && !empty($keywords)) {
            $where['name'] = ['like', '%'.$keywords.'%'];
        }
        $mini = $this->db->table('tbhero')->where($where)->select();

        $res = [
            'type'   => 1,
            'status' => 'success',
            'msg'    => "成功",
            'data'   => $mini
        ];
        $this->ajaxReturn($res, 'json');
    }

    //获取单个英雄的信息
    public function getHeroInfo()
    {
        $heroId = I('heroId');
        $hero   = $this->heroModel->getHeroInfo($heroId, $this->where);
        if ( !empty($hero)) {

            $this->ajaxSuccess("成功", $hero);

        } else {
            $this->ajaxError('获取失败');
        }
    }

    //修改英雄信息
    public function update()
    {

        $heroId    = I('heroId');
        $name      = I('name');
        $typeId    = I('typeId');
        $miniId    = I('miniId');
        $star      = I('star');
        $imagePath = I('imagePath');
        $shape     = I('shape');
        $quality   = I('quality');
        if (!is_numeric($heroId)) {
            $this->ajaxError('英雄ID必须为数字类型');
        }
        if (empty($name)) {
            $this->ajaxError('英雄名称不能为空');
        }
        if (!is_numeric($typeId)) {
            $this->ajaxError('请选择英雄类型');
        }
        if (!is_numeric($miniId)) {
            $this->ajaxError('请选择英雄所属');
        }
        if (!is_numeric($star)) {
            $this->ajaxError('英雄星级必须为数字类型');
        }
        if (empty($imagePath)) {
            $this->ajaxError('请上传图标');
        }
        if (empty($shape)) {
            $this->ajaxError('请选择英雄形态');
        }
        if (empty($quality)) {
            $this->ajaxError('请选择英雄品质');
        }

        $user     = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];
        $data     = [
            'name'       => $name,
            'typeId'     => $typeId,
            'miniId'     => $miniId,
            'star'       => $star,
            'imagePath'  => $imagePath,
            'shape'      => $shape,
            'quality'    => $quality,
            'updateMan'  => $userName,
            'updateTime' => date('Y-m-d H:i:s'),
        ];
        $res      = $this->heroModel->updateHero($heroId, $this->where, $data);

        logWrite(parent::LOG_TYPE_EDIT, $res, 'tbhero');
        if ($res !== false) {
            $this->ajaxSuccess("编辑成功");
        } else {

            $this->ajaxError('编辑失败');
        }
    }

    public function addHeroMini()
    {
        $name = I('name');
        if (empty($name)) {
            $this->ajaxError('请输入名称');
        }
        $data = ['name' => $name];
        $mini = $this->db->table('tbheromini')->where($data)->find('name');
        if ($mini) {
            $this->ajaxError('该名称已存在');
        }
        $res = $this->db->table('tbheromini')->insert($data);
        logWrite(parent::LOG_TYPE_INSENT, $res, 'tbheromini');
        if ($res !== false) {
            $this->ajaxSuccess("英雄所属成功");
        } else {
            $this->ajaxError('英雄所属失败');
        }
    }

    public function addHeroType()
    {
        $name = I('name');
        if (empty($name)) {
            $this->ajaxError('请输入名称');
        }
        $data = ['name' => $name];
        $mini = $this->db->table('tbherotype')->where($data)->find('name');
        if ($mini) {
            $this->ajaxError('该名称已存在');
        }
        $res = $this->db->table('tbherotype')->insert($data);
        logWrite(parent::LOG_TYPE_INSENT, $res, 'tbherotype');
        if ($res !== false) {
            $this->ajaxSuccess("英雄类型成功");
        } else {
            $this->ajaxError('英雄类型失败');
        }
    }

    public function addHero()
    {
        $heroId = I('heroId');
        $name = I('name');
        $imagePath = I('imagePath');
        $typeId = I('typeId');
        $miniId = I('miniId');
        $star = I('star');
        $shape = I('shape');
        $quality = I('quality');
        if (!is_numeric($heroId)) {
            $this->ajaxError('英雄ID必须为数字类型');
        }
        if (empty($name)) {
            $this->ajaxError('英雄名称不能为空');
        }
        if (!is_numeric($typeId)) {
            $this->ajaxError('请选择英雄类型');
        }
        if (!is_numeric($miniId)) {
            $this->ajaxError('请选择英雄所属');
        }
        if (!is_numeric($star)) {
            $this->ajaxError('英雄星级必须为数字类型');
        }
        if (empty($imagePath)) {
            $this->ajaxError('请上传图标');
        }
        if (empty($shape)) {
            $this->ajaxError('请选择英雄形态');
        }
        if (empty($quality)) {
            $this->ajaxError('请选择英雄品质');
        }
        $user     = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];
        $data     = [
            'heroId' => $heroId,
            'name'       => $name,
            'typeId'     => $typeId,
            'miniId'     => $miniId,
            'star'       => $star,
            'imagePath'  => $imagePath,
            'shape'      => $shape,
            'quality'    => $quality,
            'creater'    => $userName,
            'createTime' => date('Y-m-d H:i:s'),
        ];


        $where = [
            'versionId' => $this->version['id'],
            'seasonId' => $this->season['id'],
            'isDelete' => 1
        ];
        if ($this->heroModel->getHeroInfo($heroId,$where)) {
            $this->ajaxError('该英雄已存在');
        }
        $res = $this->heroModel->insertHeroOne($this->version['id'],$this->season['id'],$data);

        logWrite(parent::LOG_TYPE_INSENT, $res, 'tbhero');
        if ($res !== false) {
            $this->ajaxSuccess("编辑成功");
        } else {

            $this->ajaxError('编辑失败');
        }
    }


}